package com.zzy.service.impl;

import com.zzy.dao.BaseDao;
import com.zzy.model.Menu;
import com.zzy.model.MenuVo;
import com.zzy.service.MenuService;
import org.hibernate.Criteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户接口
 * @author zzy
 *
 */
@Transactional
@Service()
public class MenuServiceImpl implements MenuService {


    @Autowired
    private BaseDao<Menu> basedao;


    /**
     * 根据HQL和条件
     * 获取User实体
     * @param hql
     * @param param
     * @return
     */
    public Menu getUnique(String hql, Object param[]){
        return basedao.get(hql,param);
    }
    /**
     * 根据UID获取用户实体
     * @param id
     * @return Menu
     */
    public Menu getByID(String id){
        return  basedao.get(Menu.class,id);
    }
    /**
     * 分页获取 用户 列表
     * @param hql
     * @param param
     * @param page
     * @param rows
     * @return List<Menu>
     */
    public List<Menu> getPage(String hql, Object param[], Integer page, Integer rows){
        return basedao.find(hql, param, page, rows);
    }
    public List<Menu> getPage(Criteria cr, Integer page, Integer rows){
        return basedao.find(cr,page,rows);
    }
    public List<Menu> getList(String hql, Object param[]){
        return basedao.find(hql, param);
    }
    public List<Menu> getList(Criteria cr){
        return basedao.find(cr);
    }
    /**
     * 获取List的条数
     * "select count(*) from Menu where ... "
     * 适用于hql 查询
     */
    public Integer getTotalNum(String hql, Object param[]){
        long k = basedao.count(hql,param);
        return (int) k;
    }

    /**
     * 添加新用户
     * 1成功-1失败
     * @param depart
     * @return int
     */
    public Integer save(Menu depart){
        int i = 1;
        try {
            basedao.saveOrUpdate(depart);
        } catch (Exception e) {
            i =  -1;
        }
        return i;
    }
    /**
     * 根据Uid删除用户
     * 1成功-1失败
     * @param id
     * @return int
     */
    public Integer del(String id){
        int i = 1;
        try {
            if (StringUtils.hasText(id)){
                for(String did : id.split(",")){
                    Menu o = new Menu();
                    o=this.getByID(did);
                    basedao.delete(o);
                }
            }
        } catch (Exception e) {
            i = -1;
        }
        return i;
    }

    public Integer delTree(String id){
        int i = 1;
        try {
            if (StringUtils.hasText(id)){
                for(String did : id.split(",")){
                    Menu o = new Menu();
                    o=this.getByID(did);
                    delListNext( did );//删除前先删除上级的，在删除自己
                    basedao.delete(o);
                }
            }
        } catch (Exception e) {
            i = -1;
        }
        return i;
    }
    public void delListNext(String parentId) {
        String hql = "from Menu where 1=1 ";
        Object[] val = new Object[]{} ;
        if (StringUtils.hasText(parentId)) {
            hql += " and parentId = ? ";
            val = new Object[]{parentId} ;
        } else {
            hql += "and parentId = '' or parentId is null ";
        }
        List<Menu> list = basedao.find(hql,val);
        if(!list.isEmpty()){
            for(Menu d : list){
                delTree(d.getId());
            }
        }

    }



    /**
     * 更新
     * 1成功-1失败
     * @param depart
     * @return int
     */
    public Integer update(Menu depart){
        int i = 1;
        try {
            basedao.update(depart);
        } catch (Exception e) {
            i =  -1;
            e.printStackTrace();
        }
        return i;
    }
    /**
     * 保存或者更新
     * 1成功-1失败
     * @param depart
     * @return int
     */
    public Integer saveOrUpdate(Menu depart){
        int i = 1;
        try {
            basedao.saveOrUpdate(depart);
        } catch (Exception e) {
            i =  -1;
        }
        return i;
    }





    public String getTreeHtml(String parentId) {
        String treeHTml = "<ul id='shu' class='shu_files_ul'>";
        if(parentId!=null){
            treeHTml = "<ul role='group'>";
        }
        List<Menu> roots = (List) listNextHtml(parentId);
        for (Menu root : roots) {
            String name = root.getName();
            String id= root.getId();
            treeHTml +="<li id='"+id+"' >"
                +"<a href='javascript:void(0);'>"
                + name
                +"<div class='shu_files_icon'>"
                +"<span onclick='addNode(\""+id+"\");' class='shu_files_icon1'></span>&nbsp;&nbsp;&nbsp;"
                +"<span onclick='updateNode(\""+id+"\");' class='shu_files_icon2'></span>&nbsp;&nbsp;&nbsp;"
                +"<span onclick='delNode(\""+id+"\");' class='shu_files_icon3'></span>"
                +"</div>"
                +"</a>"
                //加载第二级
                + getTreeHtml(id)
                +"</li>";
        }
        treeHTml += "</ul>";
        return treeHTml;
    }

    public List<Menu> listNextHtml(String parentId) {
        String hql = "from Menu where 1=1 ";
        Object[] val = new Object[]{} ;
        if (StringUtils.hasText(parentId)) {
            hql += " and parentId = ? ";
            val = new Object[]{parentId} ;
        } else {
            hql += "and parentId = '' or parentId is null ";
        }
        return basedao.find(hql,val);
    }






    public List<MenuVo> getTree(String parentId) {
        List<MenuVo> vos = new ArrayList<MenuVo>();
        List<Menu> roots = listNext(parentId);
        for (Menu root : roots) {
            MenuVo vo = new MenuVo();
            vo.setId(root.getId());
            vo.setName(root.getName());
            vo.setUrl(root.getUrl());
            vo.setChildNodes(getTree(root.getId()));
            vos.add(vo);
        }
        return vos;
    }

    public List<Menu> listNext(String parentId) {
        String hql = "from Menu where 1=1 ";
        Object[] val = new Object[]{} ;
        if (StringUtils.hasText(parentId)) {
            hql += " and parentId = ? ";
            val = new Object[]{parentId} ;
        } else {
            hql += "and parentId = '' or parentId is null ";
        }
        return basedao.find(hql,val);
    }

}

